home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Framewrk / FWViews / Sources / FWPopup.cpp < prev    next >
Encoding:
Text File  |  1996-08-16  |  10.0 KB  |  350 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWPopup.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFrameW.hpp"
  11.  
  12. #ifndef FWPOPUP_H
  13. #include "FWPopup.h"
  14. #endif
  15.  
  16. #ifndef FWFRAME_H
  17. #include "FWFrame.h"
  18. #endif
  19.  
  20. #ifndef FWPART_H
  21. #include "FWPart.h"
  22. #endif
  23.  
  24. #ifndef FWNOTDEF_H
  25. #include "FWNotDef.h"
  26. #endif
  27.  
  28. #ifndef FWCONTXT_H
  29. #include "FWContxt.h"
  30. #endif
  31.  
  32. // ----- OS Layer -----
  33.  
  34. #ifndef FWEVENT_H
  35. #include "FWEvent.h"
  36. #endif
  37.  
  38. #ifndef FWRRCSHP_H
  39. #include "FWRRcShp.h"
  40. #endif
  41.  
  42. #ifndef FWODGEOM_H
  43. #include "FWODGeom.h"
  44. #endif
  45.  
  46. // ----- Foundation Includes -----
  47.  
  48. #ifndef FWCFMRES_H
  49. #include "FWCFMRes.h"
  50. #endif
  51.  
  52. #ifndef FWSTREAM_H
  53. #include "FWStream.h"
  54. #endif
  55.  
  56. #ifndef FWNOTIFN_H
  57. #include "FWNotifn.h"
  58. #endif
  59.  
  60. #ifndef FWNOTDEF_H
  61. #include "FWNotDef.h"
  62. #endif
  63.  
  64. // ----- OpenDoc Includes -----
  65.  
  66. #ifndef SOM_ODSession_xh
  67. #include <ODSessn.xh>
  68. #endif
  69.  
  70. #ifndef SOM_ODFacet_xh
  71. #include <Facet.xh>
  72. #endif
  73.  
  74. #ifndef SOM_ODTransform_xh
  75. #include <Trnsform.xh>
  76. #endif
  77.  
  78. //========================================================================================
  79. // File scope definitions
  80. //========================================================================================
  81.  
  82. #ifdef FW_BUILD_MAC
  83. #pragma segment fwgadgts
  84. #endif
  85.  
  86. #if defined(__MWERKS__) && GENERATING68K
  87. // A hack to work around a bug
  88. #pragma import list somGetGlobalEnvironment
  89. #endif
  90.  
  91. //========================================================================================
  92. // CLASS FW_CPopupMenu
  93. //========================================================================================
  94.  
  95. FW_DEFINE_AUTO(FW_CPopupMenu)
  96. FW_DEFINE_CLASS_M1(FW_CPopupMenu, FW_CNativeControl)
  97.  
  98. // This class is archivable, but we provide the archiving implementation in a separate
  99. // translation unit in order to enable deadstripping of the archiving-related code
  100. // in parts that do not use archiving with this class.
  101.  
  102. //----------------------------------------------------------------------------------------
  103. // FW_CPopupMenu::FW_CPopupMenu
  104. //----------------------------------------------------------------------------------------
  105.  
  106. FW_CPopupMenu::FW_CPopupMenu(Environment* ev, 
  107.                                 FW_CSuperView* container, ODID id,
  108.                                 const FW_CRect& bounds,
  109.                                 FW_ResourceId menuId,
  110.                                 const FW_CString& title,
  111.                                    short titleWidth,
  112.                                   short initialMenuItem,
  113.                                   FW_PopupMenuStyle popupVariation,
  114.                                 const FW_CFont& font,
  115.                                 long popupRefCon) :
  116.     FW_CNativeControl(ev, container, id, bounds, FW_kPopupClickedMsg)
  117. {
  118.     Initialize(ev, menuId, title, titleWidth, initialMenuItem, popupVariation, font, popupRefCon);
  119. }
  120.  
  121. //----------------------------------------------------------------------------------------
  122. //    FW_CPopupMenu::FW_CPopupMenu
  123. //----------------------------------------------------------------------------------------
  124.  
  125. FW_CPopupMenu::FW_CPopupMenu(Environment* ev) :
  126.     FW_CNativeControl(ev)
  127. {
  128. }
  129.  
  130. //----------------------------------------------------------------------------------------
  131. // FW_CPopupMenu::~FW_CPopupMenu
  132. //----------------------------------------------------------------------------------------
  133.  
  134. FW_CPopupMenu::~FW_CPopupMenu()
  135. {
  136. }
  137.  
  138. //----------------------------------------------------------------------------------------
  139. //    FW_CPopupMenu::Initialize
  140. //----------------------------------------------------------------------------------------
  141.  
  142. void FW_CPopupMenu::Initialize(Environment* ev,
  143.                                 FW_ResourceId menuId,
  144.                                 const FW_CString& title,
  145.                                    short titleWidth,
  146.                                   short initialMenuItem,
  147.                                   FW_PopupMenuStyle popupVariation,
  148.                                 const FW_CFont& font,
  149.                                 long popupRefCon) 
  150. {
  151. #ifdef FW_BUILD_MAC
  152.     Str255 pascalText;
  153.     title.ExportPascal(pascalText);
  154.     fControlHelper = new FW_CPrivMacControlHelper (ev, this, 0, (short)menuId, titleWidth, 
  155.                                 popupMenuProc + popupVariation, pascalText, font, popupRefCon);
  156.  
  157.     if (GetPlatformMenuHandle() == 0)
  158.     {
  159.         // Menu wasn't loaded correctly
  160.         FW_DEBUG_MESSAGE("Missing MENU resource or incorrect menuId");
  161.     }
  162.     fControlHelper->SetValue(initialMenuItem, FALSE);
  163.     fValue = fControlHelper->GetValue();
  164. #endif
  165.  
  166. #ifdef FW_BUILD_WIN
  167.     // [TODO]
  168.     fControlHelper = new FW_CPrivWinPopupMenuHelper
  169.                                 (ev, FW_TYPEID_FROM_TYPE(FW_CPrivWinPopupMenuHelper), this);
  170.     fControlHelper->SetText(title);
  171. #endif
  172. }
  173.  
  174. //----------------------------------------------------------------------------------------
  175. // FW_CPopupMenu::GetPlatformMenuHandle
  176. //----------------------------------------------------------------------------------------
  177.  
  178. FW_PlatformHandle    FW_CPopupMenu::GetPlatformMenuHandle() const
  179. {
  180. #ifdef FW_BUILD_MAC
  181.     PopupPrivateDataHandle    h = 
  182.         (PopupPrivateDataHandle) (**(fControlHelper->GetControlHandle())).contrlData;
  183.     return (FW_PlatformHandle)(**h).mHandle;
  184. #endif
  185. }
  186.  
  187. //----------------------------------------------------------------------------------------
  188. // FW_CPopupMenu::PrivSetValue
  189. //----------------------------------------------------------------------------------------
  190.  
  191. FW_Boolean FW_CPopupMenu::PrivSetValue(Environment *ev, FW_ControlValue value, ODFacet* facet)
  192. {
  193.     // Update the native control.  Return false if value was not changed 
  194.     if (FW_CNativeControl::PrivSetValue(ev, value, facet))
  195.     {
  196.         // Send a popup menu message to its receivers
  197.         if (GetMessage(ev) != FW_kNullMsg)
  198.             Notify(ev, FW_CPopupMenuNotification(this));
  199.  
  200.         return true;
  201.     }
  202.     else
  203.         return false;
  204. }
  205.  
  206. //----------------------------------------------------------------------------------------
  207. // FW_CPopupMenu::ControlClicked
  208. //----------------------------------------------------------------------------------------
  209.  
  210. void FW_CPopupMenu::ControlClicked(Environment *ev, FW_ControlValue value, ODFacet* facet)
  211. {
  212.     PrivSetValue(ev, value, facet);
  213. }
  214.  
  215. //----------------------------------------------------------------------------------------
  216. //    FW_CPopupMenu::GetMenuString
  217. //----------------------------------------------------------------------------------------
  218.  
  219. void    FW_CPopupMenu::GetMenuString(Environment* ev, short index, FW_CString& str) const
  220. {
  221.     // [LSD] todo: add checking for valid index
  222.     
  223. #ifdef FW_BUILD_MAC
  224.     FW_CAcquireCFMResourceAccess resAccess(ev);    // open res file for MENU resource
  225.     
  226.     // force an udpate of the control handle to get the correct menu handle...
  227. #if defined(__MWERKS__) && GENERATING68K
  228.     Point zero;
  229.     ::SetPt(&zero, 0, 0);
  230. #else
  231.     Point zero = { 0, 0};
  232. #endif
  233.     ::TestControl(fControlHelper->GetControlHandle(), zero);
  234.     
  235.     MenuHandle macMenu = (MenuHandle)GetPlatformMenuHandle();
  236.     Str255 pascalStr;
  237.     
  238.     ::GetMenuItemText(macMenu, index, pascalStr);
  239.     str.ReplaceAll(pascalStr);
  240. #endif    
  241. }
  242.  
  243. //----------------------------------------------------------------------------------------
  244. //    FW_CPopupMenu::Flatten
  245. //----------------------------------------------------------------------------------------
  246.  
  247. void FW_CPopupMenu::Flatten(Environment* ev, FW_CWritableStream& stream) const
  248. {
  249.     FW_CNativeControl::Flatten(ev, stream);
  250.  
  251.     FW_CString title;
  252.     fControlHelper->GetText(title);
  253.  
  254.     // [LSD] we use default values for some fields for now.
  255.     PopupPrivateDataHandle    h = 
  256.         (PopupPrivateDataHandle) (**(fControlHelper->GetControlHandle())).contrlData;
  257.     FW_ASSERT(h);
  258.     FW_ResourceId menuId = (**h).mID;
  259.     short titleWidth = 0;
  260.     short initialMenuItem = 1;
  261.     FW_PopupMenuStyle popupVariation = 0;
  262.     long popupRefCon = 0;
  263.     FW_CFont font(FW_kSystemFont12);
  264.  
  265.     stream << menuId;
  266.     stream << titleWidth;
  267.     stream << initialMenuItem;
  268.     stream << popupVariation;
  269.     stream << popupRefCon;
  270.     stream << font;    
  271.     stream << title;
  272. }
  273.  
  274. //----------------------------------------------------------------------------------------
  275. //    FW_CPopupMenu::Initialize
  276. //----------------------------------------------------------------------------------------
  277.  
  278. void FW_CPopupMenu::InitializeFromStream(Environment* ev, FW_CReadableStream& stream)
  279. {
  280.     FW_CNativeControl::InitializeFromStream(ev, stream);
  281.  
  282.     FW_ResourceId menuId;
  283.     FW_CString title;
  284.     short titleWidth;
  285.     short initialMenuItem;
  286.     FW_PopupMenuStyle popupVariation;
  287.     FW_CFont font;
  288.     long popupRefCon;
  289.     
  290.     stream >> menuId;
  291.     stream >> titleWidth;
  292.     stream >> initialMenuItem;
  293.     stream >> popupVariation;
  294.     stream >> popupRefCon;
  295.     stream >> font;    
  296.     stream >> title;
  297.  
  298.     Initialize(ev, menuId, title, titleWidth, initialMenuItem, popupVariation, font, popupRefCon);
  299. }
  300.  
  301. //========================================================================================
  302. //    CLASS FW_CPopupMenuNotification
  303. //========================================================================================
  304.  
  305. FW_DEFINE_CLASS_M1(FW_CPopupMenuNotification, FW_CControlNotification)
  306.  
  307. //----------------------------------------------------------------------------------------
  308. // FW_CPopupMenuNotification::FW_CPopupMenuNotification
  309. //----------------------------------------------------------------------------------------
  310.  
  311. FW_CPopupMenuNotification::FW_CPopupMenuNotification(FW_CPopupMenu* popup) :
  312.     FW_CControlNotification(popup)
  313. {
  314.     Environment* ev = somGetGlobalEnvironment();
  315.     fMenuIndex = (short)popup->GetValue(ev);
  316.     popup->GetMenuString(ev, fMenuIndex, fMenuString);
  317. }
  318.  
  319. FW_CPopupMenuNotification::FW_CPopupMenuNotification(const FW_CPopupMenuNotification& other) :
  320.     FW_CControlNotification(other),
  321.     fMenuIndex(other.fMenuIndex),
  322.     fMenuString(other.fMenuString)
  323. {
  324. }
  325.  
  326. //----------------------------------------------------------------------------------------
  327. // FW_CPopupMenuNotification::~FW_CPopupMenuNotification
  328. //----------------------------------------------------------------------------------------
  329.  
  330. FW_CPopupMenuNotification::~FW_CPopupMenuNotification()
  331. {
  332. }
  333.  
  334. //----------------------------------------------------------------------------------------
  335. // FW_CPopupMenuNotification::operator=
  336. //----------------------------------------------------------------------------------------
  337.  
  338. FW_CPopupMenuNotification& FW_CPopupMenuNotification::operator=
  339.                                         (const FW_CPopupMenuNotification& other)
  340. {
  341.     if (this != &other)
  342.     {
  343.         FW_CControlNotification::operator=(other);
  344.         fMenuIndex = other.fMenuIndex;
  345.         fMenuString = other.fMenuString;
  346.     }
  347.     return *this;
  348. }
  349.  
  350.